﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using GrewalCRM.Entities;

namespace GrewalCRM.DAL
{
    public class DealersMaster_DAL : BaseDALGrewalCRM, IBaseDALGrewalCRM
    {
        #region Protected Methods
        public override int AddNew(BaseEntitiesGrewalCRM Entity)
        {
            int result = 0;
            DealersMaster dealer = Entity as DealersMaster;
            Cmd.CommandText = "usp_InsertDealersMaster";
            SetParametersForDBSave(dealer);
            Cmd.Connection.Open();
            Cmd.ExecuteNonQuery();
            result = (int)(Cmd.Parameters["@result"].Value);
            Cmd.Connection.Close();
            return result;
        }

        public override int Update(BaseEntitiesGrewalCRM Entity)
        {
            int result = 0;
            DealersMaster dealer = Entity as DealersMaster;
            Cmd.CommandText = "usp_UpdateDealersMaster";
            SetParametersForDBSave(dealer);
            Cmd.Parameters.AddWithValue("@DealerID", Entity.ID);
            Cmd.Connection.Open();
            Cmd.ExecuteNonQuery();
            result = (int)(Cmd.Parameters["@result"].Value);
            Cmd.Connection.Close();
            return result;
        }

        public override int Delete(BaseEntitiesGrewalCRM Entity)
        {
            int result = 0;
            DealersMaster dealer = Entity as DealersMaster;
            Cmd.CommandText = "usp_DeleteDealersMaster";
            SetParametersForDBSave(dealer);
            Cmd.Parameters.AddWithValue("@DealerID", Entity.ID);
            Cmd.Connection.Open();
            Cmd.ExecuteNonQuery();
            result = (int)(Cmd.Parameters["@result"].Value);
            Cmd.Connection.Close();
            return result;
        }
        #endregion

        #region Public Method
        public int Save(BaseEntitiesGrewalCRM Entity)
        {
            throw new NotImplementedException();
        }
        public override List<BaseEntitiesGrewalCRM> GetAll()
        {

            Cmd.CommandText = "usp_GetAllDEalersMaster";
            Cmd.Connection.Open();
            Rdr = Cmd.ExecuteReader();
            List<BaseEntitiesGrewalCRM> allDealer = new List<BaseEntitiesGrewalCRM>();

            if (Rdr != null && Rdr.HasRows)
            {
                while (Rdr.Read())
                {
                    UserCRM lasrModifiedUser = new UserCRM(Rdr[7] != null && Rdr[7].GetType() != typeof(DBNull) ? Rdr.GetInt32(7) : 0);
                    lasrModifiedUser.UserName = Rdr[8] != null && Rdr[8].GetType() != typeof(DBNull) ? Rdr.GetString(8) : string.Empty;

                    DealersMaster dealer = new DealersMaster(Rdr[0] != null && Rdr[0].GetType() != typeof(DBNull) ? Rdr.GetInt32(0) : 0,
                                                            Rdr[1] != null && Rdr[1].GetType() != typeof(DBNull) ? Rdr.GetString(1) : string.Empty,
                                                            Rdr[2] != null && Rdr[2].GetType() != typeof(DBNull) ? Rdr.GetString(2) : string.Empty,
                                                            Rdr[3] != null && Rdr[3].GetType() != typeof(DBNull) ? Rdr.GetString(3) : string.Empty,
                                                            Rdr[4] != null && Rdr[4].GetType() != typeof(DBNull) ? Rdr.GetString(4) : string.Empty,
                                                            Rdr[5] != null && Rdr[5].GetType() != typeof(DBNull) ? Rdr.GetDecimal(5) : 0,
                                                            Rdr[6] != null && Rdr[6].GetType() != typeof(DBNull) ? Rdr.GetBoolean(6) : false,
                                                            lasrModifiedUser,
                                                            Rdr[9] != null && Rdr[9].GetType() != typeof(DBNull) ? Rdr.GetDateTime(9) : DateTime.MinValue
                                                           );
                    allDealer.Add(dealer);
                }
            }
            Cmd.Connection.Close();

            return allDealer;
        }
        public override BaseEntitiesGrewalCRM GetByID(int ID)
        {
            throw new NotImplementedException();
        }
        public override void FillEntity(BaseEntitiesGrewalCRM Entity)
        {
            throw new NotImplementedException();
        }
        #endregion

        #region Private Method
        private void SetParametersForDBSave(BaseEntitiesGrewalCRM Entity)
        {
            DealersMaster dealer = Entity as DealersMaster;
            Cmd.Parameters.Clear();

            Cmd.Parameters.Add("@result", System.Data.SqlDbType.Int).Direction = System.Data.ParameterDirection.Output;

            if (dealer.DealerName != null && dealer.DealerName.Trim() != string.Empty)
                Cmd.Parameters.AddWithValue("@DealerName", dealer.DealerName);

            if (dealer.Counter != null && dealer.Counter.Trim() != string.Empty)
                Cmd.Parameters.AddWithValue("@Counter", dealer.Counter);

            if (dealer.Address != null && dealer.Address.Trim() != string.Empty)
                Cmd.Parameters.AddWithValue("@Address", dealer.Address);

            if (dealer.ContactNo != null && dealer.ContactNo.Trim() != string.Empty)
                Cmd.Parameters.AddWithValue("@ContactNo", dealer.ContactNo);

            if (dealer.DealerCommission != null && dealer.DealerCommission.HasValue)
                Cmd.Parameters.AddWithValue("@DealerCommission", dealer.DealerCommission);

            Cmd.Parameters.AddWithValue("@IsActive", dealer.IsActive);

            if (null != dealer.LastModifiedUser && dealer.LastModifiedUser.ID != 0)
                Cmd.Parameters.AddWithValue("@LastModifiedUser", dealer.LastModifiedUser.ID);
        }
        #endregion
    }

}
